package com.evil.kingdom.controller.config;

import com.evil.common.core.entity.R;
import com.evil.kingdom.pojo.entity.config.Config;
import com.evil.kingdom.service.config.ConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统配置接口
 *
 * @author liyang
 * @date 2024-06-02 19:17
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/config")
public class ConfigController {

    private final ConfigService configService;

    /**
     * 初始化系统配置
     *
     * @return R
     */
    @RequestMapping("/init")
    public R<String> init(String suffix) {
        configService.init(suffix);
        return R.success();
    }

    /**
     * 新增系统配置
     *
     * @param config config
     * @return R
     */
    @PostMapping("/add")
    public R<Config> add(@RequestBody @Validated Config config) {
        return R.success(configService.add(config));
    }

    /**
     * 新增系统配置
     *
     * @param config config
     * @return R
     */
    @PostMapping("/update")
    public R<Config> update(@RequestBody @Validated Config config) {
        return R.success(configService.update(config));
    }

    /**
     * 查询系统配置
     *
     * @param name name
     * @return R
     */
    @RequestMapping("/find-by-name")
    public R<Config> findByName(String name) {
        return R.success(configService.findByName(name));
    }
}
